package com.example.wxordersystem.web;

import com.example.wxordersystem.dto.OrderDTO;
import com.example.wxordersystem.enums.ResultEnum;
import com.example.wxordersystem.exception.SellException;
import com.example.wxordersystem.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;
import java.util.Map;

/**买家
 * @author 常红伟
 */
@RestController
@RequestMapping("/seller/order")
@Slf4j
public class SellerOrderController {

    @Autowired
    OrderService orderService;

    /**
     * 卖家查询所有订单列表
     * @param pageNumber
     * @param pageSize
     * @param map
     * @return
     */
    @GetMapping("/list")
    public ModelAndView list(@RequestParam(value = "page" ,defaultValue = "1") Integer page,
                             @RequestParam(value ="size" ,defaultValue = "10") Integer size,
                             Map<String,Object> map){
        List<OrderDTO> orderDTOList=orderService.findList(page,size);
        map.put("orderList",orderDTOList);
        map.put("currentPage",page);
        map.put("size",size);
        return new ModelAndView("order/list",map);
    }


    /**
     * 取消订单
     * @param orderId
     * @param map
     * @return
     */
    @GetMapping("/cancel")
    public ModelAndView cancel(@RequestParam(value ="orderId") String orderId,Map<String,Object>map){

        try {
            OrderDTO orderDTO=orderService.findOne(orderId);
            orderService.cancel(orderDTO);
        }catch (SellException e){
            log.error("【卖家端取消订单】发生异常{}",e);
            map.put("mas",e.getMessage());
            map.put("url","/sell/seller/order/list");
            return new ModelAndView("common/error",map);
        }
        map.put("msg", ResultEnum.ORDER_CANCEL_SUCCESS.getMessage());
        map.put("url","/sell/seller/order/list");
        return new ModelAndView("common/success");
    }


    /**
     * 查询订单详情
     * @param orderId
     * @return
     */
    @GetMapping("/detail")
    public ModelAndView detail(@RequestParam(value = "orderId") String orderId,Map<String,Object>map){
        OrderDTO orderDTO = new OrderDTO();
        try {
            orderDTO  =  orderService.findOne(orderId);
        }catch (SellException e){
            log.error("【卖家端取查看订单详情】发生异常{}",e);
            map.put("mas",e.getMessage());
            map.put("url","/sell/seller/order/detail");
            return new ModelAndView("common/error",map);
        }

        map.put("orderDTO",orderDTO);
        return new ModelAndView("order/detail",map);
    }
}
